package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.PageResult;
import com.dongdongshop.domain.rep.TypeTemplateReq;
import com.dongdongshop.domain.res.SpecificationOptionRes;
import com.dongdongshop.domain.res.SpecificationRes;
import com.dongdongshop.domain.res.TypeTemplateRes;
import com.dongdongshop.mapper.ItemMapper;
import com.dongdongshop.mapper.SpecOptionMapper;
import com.dongdongshop.mapper.TemplateMapper;
import com.dongdongshop.model.ItemCat;
import com.dongdongshop.model.SpecificationOption;
import com.dongdongshop.model.TypeTemplate;
import com.dongdongshop.service.TemplateService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TemplateServiceImpl implements TemplateService {
    @Autowired
    private TemplateMapper templateMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private SpecOptionMapper specOptionMapper;
    /**
     * 模板分页查询
     * @param templateReq
     * @return
     */
    @Override
    public PageResult queryPage(TypeTemplateReq templateReq) {
        TypeTemplate typeTemplate = new TypeTemplate();
        BeanUtils.copyProperties(templateReq, typeTemplate);
        PageHelper.startPage(templateReq.getPageNum(), templateReq.getPageSize());
        Page<TypeTemplate> page=templateMapper.queryPage(typeTemplate);
        return new PageResult<>(page.getPageNum(), page.getPages(), page.getTotal(), page.getResult());
    }


    /**
     * 模板增加修改操作
     * @param templateReq
     * @return
     */
    @Override
    @Transactional
    public void save(TypeTemplateReq templateReq) {
        TypeTemplate typeTemplate=new TypeTemplate();
        BeanUtils.copyProperties(templateReq,typeTemplate);
        if(templateReq.getId()==null){
            templateMapper.add(typeTemplate);
        }else {
            templateMapper.delete(typeTemplate.getId());
            templateMapper.add(typeTemplate);
        }
    }


    /**
     * 模板删除操作
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        templateMapper.delete(id);
    }

    /**
     * 模板批量删除操作
     * @param ids
     * @return
     */
    @Override
    public void batchRemove(List<Integer> ids) {
        templateMapper.batchRemove(ids);
    }

    @Override
    public TypeTemplateRes queryByItemId(Long itemId) {
        ItemCat itemCat=itemMapper.queryByItemId(itemId);
        TypeTemplate template=templateMapper.queryByItemId(itemCat.getTypeId());
        if(template!=null){
            TypeTemplateRes templateRes=new TypeTemplateRes();
            BeanUtils.copyProperties(template,templateRes);
            String specIds = templateRes.getSpecIds();
            List<SpecificationRes> resList = JSONObject.parseArray(specIds, SpecificationRes.class);
            List<Long> specIdList = resList.stream().map(SpecificationRes::getId).collect(Collectors.toList());
            List<SpecificationOption> list=specOptionMapper.queryByIds(specIdList);
            Map<Long, List<SpecificationOption>> collect = list.stream().collect(Collectors.groupingBy(SpecificationOption::getSpecId));

            for (SpecificationRes res : resList) {
                List<SpecificationOption> optionList = collect.get(res.getId());
                res.setSpecOption(optionList.stream().map(op->{
                    SpecificationOptionRes specOptionRes=new SpecificationOptionRes();
                    BeanUtils.copyProperties(op,specOptionRes);
                    return specOptionRes;
                }).collect(Collectors.toList()));
            }
            templateRes.setSpecifications(resList);
            return templateRes;
        }
        return null;
    }
}
